home *** CD-ROM | disk | FTP | other *** search
/ Universität Tübingen - 1997/1998 Winter / Universität Tübingen - Wintersemester 1997-98 - Uni-Informationssystem und Stadt-Informationssystem.iso / nds / fade.jav < prev    next >
Text File  |  1997-08-14  |  9KB  |  362 lines

  1. /***********************************************************
  2.  *
  3.  * Fade v1.05 by Giuseppe Gennaro, 1996
  4.  *
  5.  *    Feel free to modify or distribute this code as you 
  6.  *    wish, just mention the author somewhere on the page
  7.  *      you use it. It is one of my many learning attempts at
  8.  *    the java language.  Here are the parameters for the
  9.  *     applet:
  10.  *
  11.  *    <applet code="Fade.class" width="300" height="100">
  12.  *    <param name="bgcolor" value="ffffff">
  13.  *    <param name="txtcolor" value="000000">
  14.  *    <param name="changefactor" value="dx">
  15.  *    <param name="text1" value="First text">
  16.  *    <param name="url1" value="http://www.xxx.xxx">
  17.  *    <param name="font1" value="FontName,FontStyle,FontSize">
  18.  *    <param name="text2" value="Second text">
  19.  *    <param name="url2" value="http://www.xxx.xxx">
  20.  *    <param name="fontname2" value="FontName,FontStyle,FontSize">
  21.  *     ...
  22.  *    </applet>
  23.  *      
  24.  *    You should be able to change the width, height, background 
  25.  *     color, text color, etc...  Also, the text with the extension
  26.  *    number following it is affected by all other parameters with
  27.  *     that same extension number. (Just stating the obvious.) 
  28.  */
  29.  
  30. import java.awt.*;
  31. import java.lang.*;
  32. import java.net.*;
  33. import java.applet.*;
  34. import java.io.*;
  35.  
  36. //////////////////////////////////////
  37. // OTHER CLASSES
  38. //////////////////////////////////////
  39.  
  40. class Thoughts {
  41.     static int MAX = 10;
  42.     String theThoughts[] = new String[MAX];
  43.     URL theUrls[] = new URL[MAX];
  44.     Font theFonts[] = new Font[MAX];
  45.     int R, G, B;
  46.     int dr, dg, db;
  47.     int rinit, ginit, binit;
  48.     int rfinal, gfinal, bfinal;
  49.     Color bgColor;
  50.     boolean maxxed = false;
  51.     boolean darker = false;
  52.     int curr, count;
  53.  
  54.     Thoughts() {
  55.         R = G = B = 0;
  56.         dr = dg = db = 1;
  57.         rinit = ginit = binit = 0;
  58.         rfinal = gfinal = bfinal = 255;
  59.         bgColor = new Color(R,G,B);
  60.         curr = -1;
  61.         count = 0;
  62.     }
  63.  
  64.     public void SetBackground(int i, int j, int k) {
  65.         R = rinit = i;
  66.         G = ginit = j;
  67.         B = binit = k;
  68.         bgColor = new Color(R,G,B);
  69.     }
  70.  
  71.     public Color GetBackground() {
  72.         return bgColor;
  73.     }
  74.  
  75.     public void SetTextColor(int i, int j, int k)
  76.     {    rfinal = i;
  77.         gfinal = j;
  78.         bfinal = k;
  79.     }
  80.  
  81.     public void SetChangeFactor(int i) {
  82.         if(rfinal > rinit)
  83.             dr = i;
  84.         else if(rfinal == rinit)
  85.             dr = 0;
  86.         else
  87.             dr = -i;
  88.         if(gfinal > ginit)
  89.             dg = i;
  90.         else if(gfinal == ginit)
  91.             dg = 0;
  92.         else
  93.             dg = -i;
  94.         if(bfinal > binit)
  95.             db = i;
  96.         else if(bfinal == binit)
  97.             db = 0;
  98.         else
  99.             db = -i;
  100.     }
  101.  
  102.     public void AddThought(String idea, String url, String fontname, int fontstyle, int fontsize) {
  103.         if(curr < MAX)
  104.         {    curr++;
  105.             count++;
  106.             theThoughts[curr] = idea;
  107.             try{
  108.                 theUrls[curr] = new URL(url);
  109.             }catch(MalformedURLException e) {}
  110.             theFonts[curr] = new Font(fontname, fontstyle, fontsize);
  111.         }
  112.     }
  113.  
  114.     public void Reset() {
  115.         curr = 0;
  116.     }
  117.  
  118.     public void Next() {
  119.         curr++;
  120.         if(curr >= count) Reset();
  121.     }
  122.  
  123.     public void DrawThoughts(Fade that, Graphics g) {
  124.         FontMetrics fm = that.getFontMetrics(theFonts[curr]);
  125.         Color temp = new Color(R,G,B);
  126.         g.setColor(temp);
  127.         g.setFont(theFonts[curr]);
  128.         g.drawString(theThoughts[curr],
  129.                 (that.size().width - fm.stringWidth(theThoughts[curr]))/2,
  130.                 (that.size().height + fm.getAscent())/2);
  131.     }
  132.     
  133.     // (that.size().height + fm.getHeight())/2
  134.  
  135.     public URL GetCurrentURL() {
  136.         return theUrls[curr];
  137.     }
  138.  
  139.     public int ChangeColors() {
  140.         int pause = 1;
  141.  
  142.         if(!maxxed)
  143.         {  // Adjust colors to fade in...
  144.             R += dr;
  145.             G += dg;
  146.             B += db;
  147.             if(!((dr > 0 && R < rfinal) || (dr < 0 && R > rfinal)))
  148.                 R = rfinal;
  149.             if(!((dg > 0 && G < gfinal) || (dg < 0 && G > gfinal)))
  150.                 G = gfinal;
  151.             if(!((db > 0 && B < bfinal) || (db < 0 && B > bfinal)))
  152.                 B = bfinal;
  153.             if(R == rfinal && G == gfinal && B == bfinal)
  154.             {    maxxed = true;
  155.                 pause = 10;
  156.             }
  157.         }
  158.         else
  159.         {  // Adjust colors to fade out...
  160.             R -= dr;
  161.             G -= dg;
  162.             B -= db;
  163.             if(!((dr > 0 && R > rinit) || (dr < 0 && R < rinit)))
  164.                 R = rinit;
  165.             if(!((dg > 0 && G > ginit) || (dg < 0 && G < ginit)))
  166.                 G = ginit;
  167.             if(!((db > 0 && B > binit) || (db < 0 && B < binit)))
  168.                 B = binit;
  169.             if(R == rinit && G == ginit && B == binit)
  170.             {    maxxed = false;
  171.                 pause = 10;
  172.                 Next();
  173.             }
  174.         }
  175.         return pause;
  176.     }
  177.  
  178. }
  179.  
  180. //------------------------------------
  181.  
  182. public class Fade extends java.applet.Applet implements Runnable {
  183.     Thoughts thoughts = new Thoughts();
  184.     Thread runner = null;
  185.  
  186.     public void init() {
  187.         // Set the background...
  188.         String bgRGB = getParameter("bgcolor");
  189.         if(bgRGB == null || bgRGB.length() != 6)
  190.         {    thoughts.SetBackground(0,0,0);
  191.         }
  192.         else
  193.         {    thoughts.SetBackground(HexToInt(bgRGB.substring(0,2)),
  194.                         HexToInt(bgRGB.substring(2,4)),
  195.                         HexToInt(bgRGB.substring(4)));
  196.         }
  197.         setBackground(thoughts.GetBackground());
  198.  
  199.         // Set the text color...
  200.         String txtRGB = getParameter("txtcolor");
  201.         if(txtRGB == null || txtRGB.length() != 6)
  202.         {    thoughts.SetTextColor(255,255,255);
  203.         }
  204.         else
  205.         {
  206.             thoughts.SetTextColor(HexToInt(txtRGB.substring(0,2)),
  207.                         HexToInt(txtRGB.substring(2,4)),
  208.                         HexToInt(txtRGB.substring(4)));
  209.         }
  210.  
  211.         // Set the delta for the changing color...
  212.         String changeFactor = getParameter("changefactor");
  213.         if(changeFactor == null)
  214.             thoughts.SetChangeFactor(1);
  215.         else
  216.             thoughts.SetChangeFactor(Integer.valueOf(changeFactor).intValue());
  217.  
  218.         // Obtaining the data for the thoughts...
  219.         GetThoughts(this);
  220.  
  221.         thoughts.Reset();
  222.         resize(size().width, size().height);
  223.     }
  224.  
  225.     public void start() {
  226.         if(runner == null)
  227.         {    runner = new Thread(this);
  228.             runner.setPriority(runner.MIN_PRIORITY);
  229.             runner.start();
  230.         }
  231.     }
  232.  
  233.     public void stop() {
  234.         runner = null;
  235.     }
  236.  
  237.     public void paint(Graphics g) {
  238.     }
  239.  
  240.     public void update(Graphics g) {
  241.         thoughts.DrawThoughts(this, g);
  242.     }
  243.  
  244.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  245.         super.getAppletContext().showDocument(thoughts.GetCurrentURL());
  246.         return true;
  247.     }
  248.  
  249.     public boolean mouseEnter(java.awt.Event evt, int x, int y) {
  250.         // Showing who made this...
  251.         showStatus("Fade.java by Giuseppe Gennaro");
  252.         return true;
  253.     }
  254.  
  255.     public void run() {
  256.         int sleepfactor;
  257.  
  258.         while(runner != null)
  259.         {  sleepfactor = thoughts.ChangeColors();
  260.             repaint();
  261.             try{runner.sleep(25*sleepfactor);}catch(InterruptedException e) {}
  262.         }
  263.     }
  264.  
  265.     public String getAppletInfo() {
  266.         return "Fade by Giuseppe Gennaro";
  267.     }
  268.  
  269.     ////////////////////////////////////////
  270.     // OTHER FUNCTIONS
  271.     ////////////////////////////////////////
  272.  
  273.     public void GetThoughts(Fade that){
  274.         boolean done = false;
  275.         int i=1;
  276.  
  277.         while(!done)
  278.         {    String extension = String.valueOf(i);
  279.             
  280.             String fontParam = "font" + extension;
  281.             String fontName;
  282.             int fontSize, fontStyle;
  283.             String textParam = "text" + extension;
  284.             String urlParam = "url" + extension;
  285.     
  286.             String font = super.getParameter(fontParam);
  287.             if(font == null)
  288.             {    fontName = "TimesRoman";
  289.                 fontSize = 12;
  290.                 fontStyle = Font.PLAIN;
  291.                 done = true;
  292.             }
  293.             else
  294.             {    int comma1 = font.indexOf(","),
  295.                     comma2 = font.lastIndexOf(",");
  296.                 String name = font.substring(0, comma1);
  297.                 String style = font.substring(comma1+1, comma2);
  298.                 String size = font.substring(comma2+1);
  299.                 
  300.                 fontName = name;
  301.                 fontSize = Integer.valueOf(size).intValue();
  302.                 if(style.equalsIgnoreCase("PLAIN"))
  303.                     fontStyle = Font.PLAIN;
  304.                 else if(style.equalsIgnoreCase("BOLD"))
  305.                     fontStyle = Font.BOLD;
  306.                 else if(style.equalsIgnoreCase("ITALIC"))
  307.                     fontStyle = Font.ITALIC;
  308.                 else
  309.                     fontStyle = Font.PLAIN;
  310.             }
  311.  
  312.             String theText = that.getParameter(textParam);
  313.             if(theText == null)
  314.             {    theText = "No Text Given.";
  315.                 done = true;
  316.             }
  317.             String theUrl = that.getParameter(urlParam);
  318.  
  319.             if(!done)
  320.                 thoughts.AddThought(theText, theUrl, fontName, fontStyle, fontSize);
  321.             i++;
  322.         }
  323.     }
  324.  
  325.     public int HexToInt(String value) {
  326.         int answer = 0;
  327.  
  328.         if(value.substring(0,1).equalsIgnoreCase("a"))
  329.             answer = 160;
  330.         else if(value.substring(0,1).equalsIgnoreCase("b"))
  331.             answer = 176;
  332.         else if(value.substring(0,1).equalsIgnoreCase("c"))
  333.             answer = 192;
  334.         else if(value.substring(0,1).equalsIgnoreCase("d"))
  335.             answer = 208;
  336.         else if(value.substring(0,1).equalsIgnoreCase("e"))
  337.             answer = 224;
  338.         else if(value.substring(0,1).equalsIgnoreCase("f"))
  339.             answer = 240;
  340.         else
  341.             answer = Integer.valueOf(value.substring(0,1)).intValue() * 16;
  342.  
  343.         if(value.substring(1).equalsIgnoreCase("a"))
  344.             answer += 10;
  345.         else if(value.substring(1).equalsIgnoreCase("b"))
  346.             answer += 11;
  347.         else if(value.substring(1).equalsIgnoreCase("c"))
  348.             answer += 12;
  349.         else if(value.substring(1).equalsIgnoreCase("d"))
  350.             answer += 13;
  351.         else if(value.substring(1).equalsIgnoreCase("e"))
  352.             answer += 14;
  353.         else if(value.substring(1).equalsIgnoreCase("f"))
  354.             answer += 15;
  355.         else
  356.             answer += Integer.valueOf(value.substring(1)).intValue();
  357.  
  358.         return answer;
  359.     }
  360.  
  361. }
  362.